package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.vo.PaymentSheetFromBalanceListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.paymentbalance.pojo.dto.PaymentSheetFromBalanceDTO;
import com.xbongbong.pro.paymentbalance.pojo.vo.PaymentSheetFromBalanceVO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.PaymentBalanceService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 梁鲁江
 * @version v1.0
 * @date 2019/11/20 19:04
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/paymentBalance")
public class PaymentBalanceController {
    @Resource
    private PaymentBalanceService paymentBalanceService;
    @RequestMapping(value = "/paymentSheet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String paymentUnGet(@RequestBody @Valid PaymentSheetFromBalanceDTO paymentSheetFromBalanceDTO, BindingResult br) {
        XbbResponse<PaymentSheetFromBalanceListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.PAYMENT_SHEET.getCode(), null, paymentSheetFromBalanceDTO.getDistributorMark());
                PaymentSheetFromBalanceListVO paasFormDataEsListVO = paymentBalanceService.paymentSheetFromBalanceList(paymentSheetFromBalanceDTO);
                response = new XbbResponse<>(paasFormDataEsListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取某个客户的余额
     * @param paymentSheetFromBalanceDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @date 19/12/10 010 14:39
     * @update 19/12/10 010 14:39
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/getByCustomerId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getByCustomerId(@RequestBody @Valid PaymentSheetFromBalanceDTO paymentSheetFromBalanceDTO, BindingResult br) {
        XbbResponse<PaymentSheetFromBalanceVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PaymentSheetFromBalanceVO paymentSheetFromBalanceVO = paymentBalanceService.getByCustomerId(paymentSheetFromBalanceDTO);
                response = new XbbResponse<>(paymentSheetFromBalanceVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
